package com.hmy.infrastructure.client.dingtalk;

import com.hmy.ccp.common.result.FeignResult;
import com.hmy.ccp.common.result.Results;
import com.hmy.ccp.error.annotations.HmyFeignController;
import com.hmy.infrastructure.api.bo.request.dingtalk.DeptBo;
import com.hmy.infrastructure.api.bo.request.dingtalk.GetUserRequest;
import com.hmy.infrastructure.api.bo.request.dingtalk.UserBo;
import com.hmy.infrastructure.api.bo.response.dingtalk.AppTokenResponse;
import com.hmy.infrastructure.api.bo.response.dingtalk.FailResult;
import com.hmy.infrastructure.common.utils.DingTalkUtil;
import com.hmy.infrastructure.service.dingtalk.IDingTalkDeptService;
import com.hmy.infrastructure.service.dingtalk.IDingTalkUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.net.URISyntaxException;
import java.util.List;

@RestController
@HmyFeignController
public class DingTalkController {

    @Resource
    private IDingTalkUserService dingTalkUserService;

    @Resource
    private IDingTalkDeptService dingTalkDeptService;

    @Resource
    private DingTalkUtil dingTalkUtil;

    @GetMapping("/feign/dingTalk/getAccessToken")
    public FeignResult<AppTokenResponse> getAccessToken() throws URISyntaxException {
        return Results.newSuccessResult(dingTalkUtil.getAccessToken());
    }

    @GetMapping("/feign/dingTalk/getCallBackFail")
    public FeignResult<List<FailResult>> getCallBackFail() {
        return Results.newSuccessResult(dingTalkDeptService.getCallBackFail());
    }

    @GetMapping("/feign/dingTalk/getUserDetail/{userId}")
    public FeignResult<UserBo> getUserDetail(@PathVariable("userId") String userId) {
        return Results.newSuccessResult(dingTalkUserService.getUserDetail(userId));
    }

    @PostMapping("/feign/dingTalk/getUserInfo")
    FeignResult<UserBo> getUserInfo(@RequestBody @Validated GetUserRequest getUserRequest)  {
        return Results.newSuccessResult(dingTalkUserService.getUserInfo(getUserRequest));
    }

    @GetMapping("/feign/dingTalk/getDeptDetail/{deptId}")
    public FeignResult<DeptBo> getDeptDetail(@PathVariable("deptId") long deptId) {
        return Results.newSuccessResult(dingTalkDeptService.getDeptDetail(deptId, null));
    }

    @GetMapping("/feign/dingTalk/syncDeptData")
    public FeignResult<List<DeptBo>> syncDeptData() {
        return Results.newSuccessResult(dingTalkDeptService.syncDeptData());
    }

    @GetMapping("/feign/dingTalk/syncUserData")
    public FeignResult<List<UserBo>> syncUserData() {
        return Results.newSuccessResult(dingTalkUserService.syncUserData());
    }

}
